package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(TravelComment travelComment) {
        //保存前将评论id设为空,防止在bean属性复制操作中将源对象id带过存到目标对象中.
        travelComment.setId(null);
        //维护关联评论
        //根据传过来的游记评论,获取关联评论的id
        String refId = travelComment.getRefComment().getId();
        //判断关联评论id是否有值
        if (StringUtils.hasLength(refId)){
            //如果有,则将关联评论对象设置到该评论中
            //查询
            TravelComment refComment = this.get(refId);
            //替换
            travelComment.setRefComment(refComment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        //设置创建时间
        travelComment.setCreateTime(new Date());
        //更新
        repository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        repository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));

        }
        //当前页显示数据
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize()
                , Sort.Direction.ASC,"_id");

        return DBHelper.query(query,template,TravelComment.class,pageable,qo);
    }

    @Override
    public void saveOrUpdate(TravelComment travelComment) {
        if (StringUtils.hasLength(travelComment.getId())){
            this.update(travelComment);
        }else {
            this.save(travelComment);
        }
    }

    @Override
    public List<TravelComment> queryCommentByTravelId(String travelId) {
        return repository.findByTravelId(travelId);
    }

    @Override
    public List<TravelComment> queryNewTravelComment() {
        Query query = new Query();
        Pageable pageable = PageRequest.of(0,10, Sort.Direction.DESC,"creatTime");
        query.with(pageable);
        List<TravelComment> list = template.find(query,TravelComment.class);

        return list;
    }

}
